package drawer;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import model.TestDraw;
import model.TestDraw.Figure;

import vnctest.TestConfig;

public class Drawer {

	private TestConfig config;

	private static double figureSpeed = 0.5;
	private static int where = 360;
	final static int maxCharHeight = 15;
	final static int minFontSize = 6;
	private static Image backImage;

	final static Color red = Color.red;
	final static Color white = Color.white;

	private final static Color bg = Color.white;
	private final static Color fg = Color.black;

	final static BasicStroke stroke = new BasicStroke(2.0f);
	final static BasicStroke wideStroke = new BasicStroke(8.0f);

	final static float dash1[] = { 10.0f };
	final static BasicStroke dashed = new BasicStroke(1.0f,
			BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
	Dimension totalSize;
	FontMetrics fontMetrics;

	private TestDraw testDraw;

	public static Dimension getSize() {
		// Get the default toolkit
		Toolkit toolkit = Toolkit.getDefaultToolkit();

		// Get the current screen size
		Dimension scrnsize = toolkit.getScreenSize();
		return scrnsize;
	}

	public Drawer(TestConfig config) {
		this.config = config;
		File imageFile = new File(config.getBackground());
		if(!imageFile.exists())
			imageFile  = new File("src\\vnctest\\images\\"+config.getBackground());
		System.out.println(imageFile.getAbsolutePath());
		try {
			this.backImage = ImageIO.read(imageFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		testDraw = new TestDraw(config);
	}

	public void paint(Graphics g){
		   Graphics2D g2 = (Graphics2D) g;
		   
	        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	        
	        Dimension d = getSize();
	        
	        BufferedImage drawImage = new BufferedImage(d.width, d.height,BufferedImage.TYPE_INT_ARGB );
	        
	        Graphics2D dGraphics = (Graphics2D) drawImage.getGraphics();
	        
	        dGraphics.drawImage((BufferedImage) backImage, null, 0,0);

	        dGraphics.setStroke(stroke);
	        dGraphics.setPaint(config.getColor());
	        for(Figure figure : testDraw.getFigures()){
	        	dGraphics.fill(figure.getFigure());
	        }
	        g2.drawImage((BufferedImage) drawImage, null, 0,0);
	}
	
	public void makeMove(){
		testDraw.makeMove();
	}

}
